﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;

namespace UI
{
    public partial class ValidCodeImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string code = GenerateCheckCode();
            Session["ValidateCode"] = code;
            this.CreateCheckCodeImage(Session["ValidateCode"].ToString());

            //this.CreateCheckCodeImage( GenerateCheckCode());
        }

        void createCode()
        {
            string chkCode = string.Empty;

            //颜色列表，用于验证码、噪线、噪点

            Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };

            //字体列表，用于验证码

            string[] font = { "Gungsuh" };

            //验证码的字符集，去掉了一些容易混淆的字符

            char[] character = { '0', '1', '2', '3', '4', '5', '6', '8', '9' };

            Random rnd = new Random();

            //生成验证码字符串

            for (int i = 0; i < 4; i++)
            {

                chkCode += character[rnd.Next(character.Length)];

            }

            //保存验证码的Cookie

            HttpCookie anycookie = new HttpCookie("validateCookie");

            anycookie.Values.Add("ChkCode", chkCode);
            anycookie.Expires = DateTime.Now.AddMinutes(1);

            HttpContext.Current.Response.Cookies["validateCookie"].Values["ChkCode"] = chkCode;

            Bitmap bmp = new Bitmap(130, 30);

            Graphics g = Graphics.FromImage(bmp);

            g.Clear(Color.White);

            //画噪线

            //for (int i = 0; i < 5; i++)
            //{

            //    int x1 = rnd.Next(150);

            //    int y1 = rnd.Next(30);

            //    int x2 = rnd.Next(150);

            //    int y2 = rnd.Next(30);

            //    Color clr = color[rnd.Next(color.Length)];

            //    g.DrawLine(new Pen(clr), x1, y1, x2, y2);

            //}

            //画验证码字符串

            for (int i = 0; i < chkCode.Length; i++)
            {

                string fnt = font[rnd.Next(font.Length)];

                Font ft = new Font(fnt, 18);

                Color clr = Color.Black;

                g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 20, (float)6);

            }

            //画噪点

            //for (int i = 0; i < 100; i++)
            //{

            //    int x = rnd.Next(bmp.Width);

            //    int y = rnd.Next(bmp.Height);

            //    Color clr = color[rnd.Next(color.Length)];

            //    bmp.SetPixel(x, y, clr);

            //}

            //清除该页输出缓存，设置该页无缓存

            Response.Buffer = true;

            Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);

            Response.Expires = 0;

            Response.CacheControl = "no-cache";

            Response.AppendHeader("Pragma", "No-Cache");

            //将验证码图片写入内存流，并将其以"image/Png" 格式输出

            MemoryStream ms = new MemoryStream();
            try
            {
                bmp.Save(ms, ImageFormat.Png);

                Response.ClearContent();

                Response.ContentType = "image/Png";

                Response.BinaryWrite(ms.ToArray());

            }

            finally
            {
                //显式释放资源

                bmp.Dispose();

                g.Dispose();

            }
        }

        private string GenerateCheckCode()
        {
            int number;
            char code;
            string checkCode = String.Empty;
            System.Random random = new Random();
            for (int i = 0; i < 4; i++)
            {
                number = random.Next();
                code = (char)('0' + (char)(number % 10));
                //if (number % 2 == 0)
                //    code = (char)('0' + (char)(number % 10));
                //else
                //    code = (char)('0' + (char)(number % 10));//code = (char)('A' + (char)(number % 26));
                checkCode += code.ToString();
            }
            //    Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));


            HttpCookie anycookie = new HttpCookie("validateCookie");

            anycookie.Values.Add("ChkCode", checkCode);
            anycookie.Expires = DateTime.Now.AddMinutes(1);

            HttpContext.Current.Response.Cookies["validateCookie"].Values["ChkCode"] = checkCode;
            return checkCode;
        }

        private void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;
            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成随机生成器 
                Random random = new Random();
                //清空图片背景色 
                g.Clear(Color.White);
                //画图片的背景噪音线 
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(checkCode, font, brush, 2, 2);
                //画图片的前景噪音点 
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线 
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
    }
}
